package com.live.dengdengtv.net.callback;

import android.app.Activity;
import android.content.Context;

import com.google.gson.Gson;
import com.live.dengdengtv.bean.SimpleInfoData;
import com.live.dengdengtv.utils.Lo;
import com.live.dengdengtv.utils.Notice;
import com.live.dengdengtv.view.DialogWaiting;

import java.lang.ref.WeakReference;

import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by wyf on 2016/8/15.
 */
public class ProgressDialogCallBack extends com.zhy.http.okhttp.callback.Callback<SimpleInfoData>{
    protected WeakReference<Activity> weekActivity;
    protected DialogWaiting progressDialog;
    protected RequestNetCallback callback;

    public ProgressDialogCallBack(Activity activity, RequestNetCallback callback) {

        this.weekActivity = new WeakReference<Activity>(activity);
        this.callback = callback;
    }

//    public ProgressDialogCallBack(RequestNetCallback callback) {
//
//        this.weekActivity = new WeakReference<Activity>(null);
//        this.callback = callback;
//    }

    /**
     * UI Thread
     *
     * @param request
     */
    public void onBefore(Request request, int id)
    {
        final Activity activity = weekActivity.get();
        if (activity != null && !activity.isFinishing()) {
            progressDialog = DialogWaiting.show((Context) activity);
        }

        callback.onBefore(request,id);
    }

    /**
     * UI Thread
     *
     * @param
     */
    public void onAfter(int id)
    {
        Activity activity = weekActivity.get();
        if (activity != null && !activity.isFinishing()&& progressDialog.isShowing()) {
            if (progressDialog != null) {
                progressDialog.dismiss();
            }
        }

        callback.onAfter(id);
    }
    /**
     * UI Thread
     *
     * @param progress
     */
    public void inProgress(float progress, long total , int id)
    {
        callback.inProgress(progress,total,id);
    }

    public boolean validateReponse(Response response, int id)
    {
        return response.isSuccessful();
    }
    @Override
    public SimpleInfoData parseNetworkResponse(Response response, int id) throws Exception {
        String string = response.body().string();

        Lo.e("ProgressDialogCallBack", string);

        SimpleInfoData infoBean = new Gson().fromJson(string, SimpleInfoData.class);
        return infoBean;
    }



    @Override
    public void onError(Call call, Exception e, int id) {
        callback.onError(call,e,id);
    }

    /**
     *  callback返回false执行ProgressDialogCallBack中的onResponse代码
     *  id>1000 不提示服务器返回的"错误提示"
     *  */
    @Override
    public void onResponse(SimpleInfoData response, int id) {

        Activity activity = weekActivity.get();
        if (activity != null && !activity.isFinishing()&& progressDialog.isShowing()) {
            if (progressDialog != null) {
                progressDialog.dismiss();
            }
        }

        if(!callback.onResponse(response,id)){
            if (activity != null && !activity.isFinishing()) {
                if(response.result == 1 && id<1000){
                    Notice.alert(weekActivity.get(), response.data.getAsString());
                }
            }
        }
    }
}
